<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
  <title>文本标记</title>
  <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
  <style>
    html, body, #container {
            height: 100%;
            width: 100%;
        }
        #panel {
            position: fixed;
            background-color: white;
            max-height: 90%;
            overflow-y: auto;
            top: 10px;
            right: 10px;
            width: 280px;
        }
        #panel .amap-call {
            background-color: #009cf9;
            border-top-left-radius: 4px;
   	        border-top-right-radius: 4px;
        }
        #panel .amap-lib-driving {
	        border-bottom-left-radius: 4px;
   	        border-bottom-right-radius: 4px;
            overflow: hidden;
        }
    </style>
</head>

<body>
  <div id="container"></div>
  <div id="panel"></div>

  <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.10&key=dbbed59c78f2570991019a57dd149529&plugin=AMap.Driving"></script>
  <script type="text/javascript">
    var minePosition = null
    var map = new AMap.Map("container", {
      viewMode: '3D',
      zoom: 13,
      center: [113.658075, 34.745793],
      resizeEnable: true
    });

    // 创建纯文本标记
    var text = new AMap.Text({
      text: '郑州火车站',
      textAlign: 'center', // 'left' 'right', 'center',
      verticalAlign: 'middle', //middle 、bottom
      draggable: true,
      cursor: 'pointer',
      angle: 0,
      style: {
        'padding': '.75rem 1.25rem',
        'margin-bottom': '1rem',
        'border-radius': '.25rem',
        'background-color': 'white',
        'width': '15rem',
        'border-width': 0,
        'box-shadow': '0 2px 6px 0 rgba(114, 124, 245, .5)',
        'text-align': 'center',
        'font-size': '20px',
        'color': 'blue'
      },
      position: [113.658075, 34.745793]
    });

    text.setMap(map);


    
    //解析定位结果
    function onComplete(data) {
      console.log(data.position)
      minePosition = data.position
      var driving = new AMap.Driving({
        map: map,
        panel: "panel"
      });
      driving.search(new AMap.LngLat(data.position.lng, data.position.lat), new AMap.LngLat(113.658075, 34.745793), function (status, result) {
        // result 即是对应的驾车导航信息，相关数据结构文档请参考  https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingResult
        if (status === 'complete') {
          // log.success('绘制驾车路线完成')
        } else {
          // log.error('获取驾车数据失败：' + result)
        }
      });
    }
    //解析定位错误信息
    function onError(data) {
      document.getElementById('status').innerHTML = '定位失败'
      document.getElementById('result').innerHTML = '失败原因排查信息:' + data.message;
    }


    document.onclick = function (e) {
      
      if (e.target.className === 'amap-overlay-text-container') {
        AMap.plugin('AMap.Geolocation', function () {
          var geolocation = new AMap.Geolocation({
            enableHighAccuracy: true,//是否使用高精度定位，默认:true
            timeout: 10000,          //超过10秒后停止定位，默认：5s
            buttonPosition: 'RB',    //定位按钮的停靠位置
            buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
            zoomToAccuracy: true,   //定位成功后是否自动调整地图视野到定位点

          });
          map.addControl(geolocation);
          geolocation.getCurrentPosition(function (status, result) {
            if (status == 'complete') {
              onComplete(result)
            } else {
              onError(result)
            }
          });
        });
        
      }
    }
  </script>
</body>

</html>